package isr.cloudgate.dashboard.servlet;

import isr.google.gdata.GDataClient;

import java.util.Locale;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ServletUtilities {

  private static final String ADMIN_PASSWORD = "admin_password";
  private static final String ADMIN_USER = "admin_user";
  private static final String LANG = "lang";

  private static final Logger logger = Logger.getLogger(ServletUtilities.class
    .getName());

  public static Locale checkLanguage(HttpServletRequest request) {

    String lang = request.getParameter(LANG);

    if (lang == null)
      lang = (String) request.getSession().getAttribute(LANG);

    if (lang == null) {
      String acceptLangHeader = request.getHeader("Accept-Language");

      if (acceptLangHeader != null) {
        String[] acceptLanguages = acceptLangHeader.split(",");
        if (acceptLanguages.length > 0)
          lang = acceptLanguages[0];
      }
    }

    if (lang == null)
      lang = "ja";

    request.getSession().setAttribute(LANG, lang);

    logger.info("session language: "
      + request.getSession().getAttribute("lang"));

    return new Locale(lang);
  }

  public static void clearAdminData(HttpServletRequest request) {

    logger.info("clearing login data");

    HttpSession session = request.getSession();

    session.removeAttribute(ADMIN_USER);
    session.removeAttribute(ADMIN_PASSWORD);

    // Cookie adminUserCookie = new Cookie("admin_user", "");
    // adminUserCookie.setMaxAge(0);
    //
    // Cookie adminPasswordCookie = new Cookie("admin_password", "");
    // adminPasswordCookie.setMaxAge(0);
    //
    // response.addCookie(adminUserCookie);
    // response.addCookie(adminPasswordCookie);

  }

  public static String getAdminUser(HttpServletRequest request) {

    return (String) request.getSession().getAttribute(ADMIN_USER);

  }

  public static GDataClient getAppsClient(HttpServletRequest request) {

    logger.info("getting apps client");

    HttpSession session = request.getSession();
    String adminUser = (String) session.getAttribute(ADMIN_USER);
    String adminPassword = (String) session.getAttribute(ADMIN_PASSWORD);

    // if (request.getCookies() == null) {
    // logger.info("no cookies in request");
    // } else {
    // logger.info("looking through cookies for admin data");
    // for (Cookie cookie : request.getCookies()) {
    // logger.info("[cookie] " + cookie.getName() + " = "
    // + cookie.getValue());
    // if ("admin_user".equals(cookie.getName())) {
    // adminUser = cookie.getValue();
    // }
    // if ("admin_password".equals(cookie.getName())) {
    // adminPassword = cookie.getValue();
    // }
    // }
    // }

    logger.info("admin user: " + adminUser);

    if (adminPassword == null || adminPassword == null)
      return null;

    request.setAttribute("admin", adminUser);
    String domain = adminUser.contains("@") ? adminUser.split("@")[1] : null;

    return new GDataClient(adminUser, adminPassword, domain);

  }

  public static void setAdminData(HttpServletRequest request, String adminUser,
      String adminPassword) {

    logger.info("saving login data, admin user '" + adminUser + "'");

    HttpSession session = request.getSession();
    session.setAttribute(ADMIN_USER, adminUser);
    session.setAttribute(ADMIN_PASSWORD, adminPassword);

    // Cookie adminUserCookie = new Cookie("admin_user", adminUser);
    // adminUserCookie.setMaxAge(Integer.MAX_VALUE);
    // Cookie adminPasswordCookie =
    // new Cookie("admin_password", adminPassword);
    // adminPasswordCookie.setMaxAge(Integer.MAX_VALUE);
    // response.addCookie(adminUserCookie);
    // response.addCookie(adminPasswordCookie);

  }
}
